<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
    <meta charset="UTF-8">
    <title>会议室新增</title>
    <link rel="stylesheet" th:href="@{/pear/component/pear/css/pear.css}"/>
</head>
<body>
<form class="layui-form" method="POST" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="layui-form-item">
                <label class="layui-form-label">会议室名称</label>
                <div class="layui-input-block">
                    <input type="text" name="roomName" lay-verify="required|roomName" autocomplete="off"
                           placeholder="请输入会议室名称"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">会议室描述</label>
                <div class="layui-input-block">
                    <input type="text" name="description" lay-verify="required|description" autocomplete="off"
                           placeholder="请输入会议室描述"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">部门名称</label>
                <div class="layui-input-block">
                    <select name="departmentId" lay-verify="required|departmentId" class="layui-select"
                            id="department-select">
                        <option value="">请选择部门</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">会议室可容纳人数</label>
                <div class="layui-input-block">
                    <input type="text" name="capacity" lay-verify="required|capacity" autocomplete="off"
                           placeholder="会议室可容纳人数"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">联系人</label>
                <div class="layui-input-block">
                    <input type="text" name="contactName" lay-verify="required|contactName" autocomplete="off"
                           placeholder="联系人名称"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">电话</label>
                <div class="layui-input-block">
                    <input type="text" name="contactPhone" lay-verify="required|contactPhone" autocomplete="off"
                           placeholder="联系人电话"
                           class="layui-input" maxlength="11">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">图片</label>
                <div class="layui-input-block">
                    <input type="hidden" name="image"/>
                    <button type="button" class="layui-btn" id="ID-upload-demo-btn">
                        <i class="layui-icon layui-icon-upload"></i> 上传图片
                    </button>
                    <div style="width: 132px;">
                        <div class="layui-upload-list">
                            <img class="layui-upload-img" id="ID-upload-demo-img" style="width: 100%; height: 92px;"
                                 alt="" src="">
                            <div id="ID-upload-demo-text"></div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">最大预约天数</label>
                <div class="layui-input-block">
                    <input type="text" name="reservationMax" lay-verify="required|reservationMax" autocomplete="off"
                           placeholder="会议室最大预约天数"
                           class="layui-input">
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="layui-btn  layui-btn-sm" lay-submit=""
                    lay-filter="meetRoom-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="layui-btn layui-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<script th:src="@{/pear/component/layui/layui.js}"></script>
<script th:src="@{/pear/component/pear/pear.js}"></script>
<script th:inline="javascript">

    layui.use(['form', 'jquery'], function () {
        let baseURL = /*[[@{/}]]*/ '';
        let form = layui.form;
        let layer = layui.layer;
        let upload = layui.upload;
        let $ = layui.$;
        // 自定义校验规则
        form.verify({
            // 验证会议室名称，且为必填项
            roomName: function (value) {
                if (value.length < 1 || value.length > 20) {
                    return '会议室姓名长度应在1到50个字符之间';
                }
            },
            // 会议室描述，且为必填项
            description: function (value) {
                if (value.length < 1 || value.length > 50) {
                    return '会议室姓名长度应在1到50个字符之间';
                }
            },
            capacity: function (value) {
                if (!/^\d+$/.test(value)) {
                    return '容纳人数必须是正整数';
                }
                if (value < 1 || value > 1000) {
                    return '容纳人数必须在1到1000之间';
                }
            }
        })
        $.ajax({
            url: baseURL + 'department/list', // 后端接口地址
            type: 'GET',
            dataType: 'json',
            success: function (data) {
                var select = $('#department-select');
                select.empty().append('<option value="">请选择部门</option>');
                let depart = data.data
                $.each(depart, function (index, department) {
                    select.append('<option value="' + department.departmentId + '">' + department.departmentName + '</option>');
                });
                form.render('select');
            },
            error: function () {
                layer.msg('加载失败', {icon: 2});
            }
        });
        let avatarSize;
        $.ajax({
            url: baseURL + "setting/getSettingValue",
            type: 'get',
            data: {
                settingKey: "AVATAR_SIZE"
            },
            success: function (res) {
                avatarSize = parseInt(res.data, 10)
            }
        })
        upload.render({
            elem: '#ID-upload-demo-btn',
            url: baseURL + 'meeting/meetRoom/uploadImage',
            size: avatarSize,   //默认单位为kb
            before: function (obj) {
                // 预读本地文件示例，不支持ie8
                layer.msg('上传中', {icon: 16, time: 0});
            },
            done: function (res) {
                $('#ID-upload-demo-img').attr('src', baseURL + res.data);
                $("[name=image]").val(res.data);
                if (res.code === 1000) {
                    return layer.msg('上传成功');
                } else {
                    return layer.msg(res.msg, {icon: 2, time: 2000});
                }
            }
        });

        form.on('submit(meetRoom-save)', function (data) {
            $.ajax({
                url: baseURL + 'meeting/meetRoom/add',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    if (result.code === 1000) {
                        layer.msg(result.msg, {
                            icon: 1,
                            time: 2000
                        }, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window
                                .name)); //关闭当前页
                            parent.layui.table.reload("meetRoom-table");
                        });
                    } else {
                        layer.msg(result.msg, {
                            icon: 2,
                            time: 2000
                        }, function () {
                            parent.layui.table.reload("meetRoom-table");
                        });
                    }
                }
            })
            return false;
        });
    });
</script>
</body>
</html>